package com.example.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.PageModel;
import com.example.demo.model.Result;
import com.example.demo.service.UserService;
import com.example.demo.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author hpp
 * @since 2021-09-14
 */
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserServiceImpl userServiceImpl;
    /*通过id查询用户*/
    @GetMapping("{id}")
    public Result<User> getUserById(@PathVariable Integer id) {
        User user = userServiceImpl.getById(id);
        if(user != null) return new Result<User>(200,Result.Smessage,user);
        return new Result<User>(500,Result.Emessage, null);
    }
    /*分页查询用户列表*/
    @GetMapping("")
    public Result<PageModel<User>> getUserList(@RequestParam("query") String query, @RequestParam("pagenum") long pagenum, @RequestParam("pagesize") long pagesize) {
        IPage<User> Ipage = new Page<>(pagenum, pagesize);
        QueryWrapper<User> sectionQueryWrapper = new QueryWrapper<>();
        IPage<User> result = userServiceImpl.page(Ipage, sectionQueryWrapper.like("username", query));
        PageModel<User> userResult = new PageModel<>(result.getTotal(),result.getCurrent(),result.getRecords());
        return new Result<PageModel<User>>(200, Result.Smessage, userResult);
    }
}

